package com.github.glideplus.listener;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zoudong on 2017/2/19.
 */

public class GlideOkHttpProgress {


    private static Interceptor createInterceptor(final OkHttpProgressResponseBody.ResponseProgressListener listener) {
        return new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request request = chain.request();
                Response response = chain.proceed(request);
                return response.newBuilder()
                        .body(new OkHttpProgressResponseBody(request.url(), response.body(), listener))
                        .build();
            }
        };
    }

    public static OkHttpClient GlideProgressClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(createInterceptor(new DispatchingProgressListener()));
        return builder.build();
    }
}
